package com.cash.app.ui.other

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import com.cash.app.BuildConfig
import com.cash.app.R
import com.cash.app.base.BaseActivity
import com.cash.app.constant.AppConstant.APP_UPDATE_FORCE
import com.cash.app.custom.AppActionBar
import com.cash.app.custom.CommonDialog
import com.cash.app.databinding.AcSettingBinding
import com.cash.app.databinding.DialogAppTipBinding
import com.cash.app.manager.GlobalConfigManager
import com.cash.app.manager.LoginManager
import com.cash.app.ui.login.LoginActivity
import com.cash.app.utils.ThirdAppUtils

class SettingActivity : BaseActivity() {
    private lateinit var binding: AcSettingBinding
    private val viewModel: SettingViewModel by viewModels()

    override fun setViewBind() {
        binding = AcSettingBinding.inflate(layoutInflater)
    }

    override fun getLayoutView(): View {
        return binding.root
    }

    override fun getAppActionBar(): AppActionBar? {
        return binding.actionBar
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.appNameVersion.text =
            getString(R.string.app_name).plus(" V" + BuildConfig.VERSION_NAME)

        initClickListener()
        initObserver()
    }

    private fun initClickListener() {
        binding.apply {
            checkUpdateLl.setOnClickListener {
                checkUpdateDialog()
            }

            logoutLl.setOnClickListener {
                if (LoginManager.isLogin()) {
                    showLogoutDialog()
                }
            }
        }
    }

    private fun initObserver() {
        viewModel.quitSuccess.observe(this) {
            //Toast.makeText(this, getString(R.string.logout_success), Toast.LENGTH_SHORT).show()
            LoginActivity.start(this)
            setResult(Activity.RESULT_OK)
            finish()
        }
    }

    private fun showLogoutDialog() {
        val dialogBinding = DialogAppTipBinding.inflate(layoutInflater)
        with(dialogBinding) {
            tipContent.text = getString(R.string.setting_logout_content)
        }
        CommonDialog.Builder(this).apply {
            showTitle(true)
            setTitle(getString(R.string.setting_logout_title))
            setContentView(dialogBinding.root)
            showButtons(true)
            addButton(getString(R.string.cancel), { dialog ->
                dialog.dismiss()
            }, R.color.black_1)
            addButton(getString(R.string.confirm), { dialog ->
                viewModel.appQuit()
                dialog.dismiss()
            }, R.color.yellow_4)
            setCancelable(true)
            setCancelableOnTouchOutside(true)
        }.build().show()
    }

    private fun checkUpdateDialog() {
        if (GlobalConfigManager.isNeedUpdate()) {
            showUpdateDialog()
        } else {
            Toast.makeText(this, getString(R.string.check_update_not), Toast.LENGTH_SHORT).show()
        }
    }

    private fun showUpdateDialog() {
        val dialogBinding = DialogAppTipBinding.inflate(layoutInflater)
        val version = GlobalConfigManager.getLatestVersion()
        with(dialogBinding) {
            tipContent.text = getString(R.string.check_update_tip, version)
        }
        val isForce = GlobalConfigManager.getUpdateType() == APP_UPDATE_FORCE
        CommonDialog.Builder(this).apply {
            showTitle(true)
            setTitle(getString(R.string.check_update_title))
            setContentView(dialogBinding.root)
            showButtons(true)
            if (!isForce) {
                addButton(getString(R.string.check_update_later), { dialog ->
                    dialog.dismiss()
                }, R.color.black_1)
            }
            addButton(getString(R.string.check_update_now), { dialog ->
                ThirdAppUtils.gotoGP(this@SettingActivity)
                dialog.dismiss()
            }, R.color.yellow_4)
            setCancelable(!isForce)
            setCancelableOnTouchOutside(!isForce)
        }.build().show()
    }
} 